<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('制度制定') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('制度名称')" prop="systemName">
        <el-input
          v-model="queryParams.systemName"
          :placeholder="$t('制度名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('申请人')" prop="userId">
        <UserSelect v-model="queryParams.userId" :placeholder="$t('申请人')" />
      </el-form-item>
      <el-form-item :label="$t('制度类型')" prop="systemType">
        <el-select v-model="queryParams.systemType" size="small" clearable :placeholder="$t('请选择')">
          <el-option
            v-for="dict in systemTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:systemMake:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="oaSystemMakeList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('制度名称')" align="center" prop="systemName" min-width="160" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link type="primary" @click="updateHandle(scope.row)">{{ scope.row.systemName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请人')" align="center" prop="userName" min-width="120" show-overflow-tooltip />
      <el-table-column :label="$t('制度类型')" align="center" :formatter="systemTypeFormat" prop="systemType" min-width="120" show-overflow-tooltip />
      <el-table-column :label="$t('制度简介')" align="center" prop="introduction" min-width="120" show-overflow-tooltip />
      <el-table-column :label="$t('制度创建时间')" align="center" prop="startTime" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('制度生效时间')" align="center" prop="effectTime" min-width="130">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('制度级别')" align="center" :formatter="systemLevelFormat" prop="systemLevel" min-width="100" show-overflow-tooltip />
      <el-table-column fixed="right" :label="$t('审批状态')" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <!-- 启动流程，终止流程，修改，删除 四个按钮 -->
          <div style="width: 200px; text-align: center;">
            <ListBtn :bus-data="scope.row" :other-btn="!(scope.row.actStatus === 'W' || scope.row.lastHistoricalId)" has-permi="oa:systemMake" />
            <div>
              <el-button
                v-if="scope.row.actStatus === 'W'"
                v-hasPermi="['deal:systemMake:edit']"
                size="small"
                type="text"
                icon="el-icon-brush"
                @click="systemChange(scope.row)"
              >{{ $t('制度变更') }}</el-button>
              <el-button
                v-if="scope.row.actStatus === 'W' || scope.row.actStatus === 'D'"
                v-hasPermi="['deal:systemMake:view']"
                size="small"
                type="text"
                icon="el-icon-view"
                style="margin-left: 10px;"
                @click="historicalSystemView(scope.row)"
              >{{ $t('变更历史') }}</el-button>
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改制度制定对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <oaSystemMakeForm ref="oaSystemMakeForm" />
    </el-dialog>

    <!-- 制度变更对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="historyOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="folderOpen" append-to-body>
      <oaSystemMakeHistory ref="oaSystemMakeHistory" />
    </el-dialog>
  </div>
</template>

<script>
import { listOaSystemMake, delOaSystemMake, terminateChanges } from '@/api/oa/systemMake/oaSystemMake'
import oaSystemMakeForm from './oaSystemMakeForm.vue'
import oaSystemMakeHistory from '@/views/oa/systemMake/oaSystemMakeHistory.vue'
export default {
  components: {
    oaSystemMakeForm,
    oaSystemMakeHistory
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 制度制定表格数据
      oaSystemMakeList: [],
      systemTypeOptions: [],
      systemLevelOptions: [],
      // 弹出层标题
      title: '制度制定',
      // 是否显示弹出层
      formOpen: false,
      historyOpen: false,
      // 查询参数
      queryParams: {
        systemName: undefined,
        userId: undefined,
        departmentId: undefined,
        systemType: undefined,
        startTime: undefined,
        effectTime: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getDicts('system_type').then(response => {
      this.systemTypeOptions = response.data
    })
    this.getDicts('system_level').then(response => {
      this.systemLevelOptions = response.data
    })
    this.getList()
  },
  methods: {
    /** 查询制度制定列表 */
    getList() {
      this.loading = true
      listOaSystemMake(this.queryParams).then(response => {
        this.oaSystemMakeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    systemTypeFormat(row) {
      return this.$t(this.selectDictLabel(this.systemTypeOptions, row.systemType))
    },
    systemLevelFormat(row) {
      return this.$t(this.selectDictLabel(this.systemLevelOptions, row.systemLevel))
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        systemName: undefined,
        userId: undefined,
        departmentId: undefined,
        startTime: undefined,
        effectTime: undefined,
        systemType: undefined,
        systemLevel: undefined,
        partakeUser: undefined,
        introduction: undefined,
        actId: undefined,
        actStatus: undefined,
        actTitle: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.title = '制度制定'
      this.$nextTick(() => {
        this.$refs.oaSystemMakeForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.title = '制度制定'
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.oaSystemMakeForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delOaSystemMake(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    // 制度变更
    systemChange(row) {
      this.title = '制度制定变更'
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.oaSystemMakeForm.changeBtnStatus = true
        this.$refs.oaSystemMakeForm.initForm({ id: row.id })
      })
    },
    // 制度历史
    historicalSystemView(row) {
      console.log(row)
      this.title = '制度变更史'
      this.historyOpen = true
      this.$nextTick(() => {
        this.$refs.oaSystemMakeHistory.queryParams.lastHistoricalId = row.id
        this.$refs.oaSystemMakeHistory.queryParams.delFlag = '3'
        this.$refs.oaSystemMakeHistory.getList()
      })
    },
    // 终止变更
    stopProcessCurrent(that, actId, dataId) {
      console.log('终止变更', that, actId)
      terminateChanges(dataId).then(() => {
        this.getList()
      })
    }
  }
}
</script>
